package
{
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DTriangleFace;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.textures.Texture;
	

	/**
	 * 
	 * @author Polar
	 * @date 2013-5-23
	 * @function 
	 * 
	 */
	[SWF(width="640",height="480",frameRate="60",backgroundColor="#FFFFFF")]
	public class Tutorial_02 extends Main3D
	{
		[Embed(source="res/texture.jpg")]
		private static const JPGTex:Class;
		
		private var tex:Texture;
		
		
		private static const RADIAN:Number =    180 / Math.PI;
		
		private var _iterators:int = 0;
		private var _counter:Number = 0;
		private var _halfWidth:Number;
		
		public var speed:Number = .001;
		public function Tutorial_02()
		{
			super();
		}
		
		override protected function draw():void
		{
			_iterators = 0;
			
			setVertexData(8,new <Number>
				[
					-0.9,	0.9,	0,	someCLR(),someCLR(),someCLR(),	0,0,
					0.9,	0.9,	0,	someCLR(),someCLR(),someCLR(),	1,0,
					-0.9,	-0.9,	0,	someCLR(),someCLR(),someCLR(),	0,1,
					0.9,	-0.9,	0,	someCLR(),someCLR(),someCLR(),	1,1,
//					-0.9,	0.9,	0,		1,0,0,
//					0.9,	0.9,	0,		0,1,0,
//					-0.9,	-0.9,	0,		0,0,1,
//					0.9,	-0.9,	0,		0,1,1,
				]);
			
			speed = (_halfWidth - stage.mouseX)*0.0001;
			_counter += speed;
			
			context3D.setVertexBufferAt(0,_vertexBuffer,0,Context3DVertexBufferFormat.FLOAT_3);
			context3D.setVertexBufferAt(1,_vertexBuffer,3,Context3DVertexBufferFormat.FLOAT_3);
			context3D.setVertexBufferAt(2,_vertexBuffer,6,Context3DVertexBufferFormat.FLOAT_2);
			context3D.setCulling(Context3DTriangleFace.NONE);
			context3D.setTextureAt(0,tex);
			context3D.drawTriangles(_indexBuffer,0,2);
		}
		
		override protected function main():void
		{
			backgroundColor = 0x444444;
			_halfWidth = _swfWidth>>1;
			
			tex = generateMipMap(new JPGTex,512);
			
			setIndexData(new <uint>[0,1,2,2,1,3]);
			
			shader = new Shader2Vertices3Colors;
			addLabel("Tutorial-EasyAGAL");
		}
		
		private function someCLR():Number
		{
			return .5 + Math.cos((_counter * 12 + _iterators++) * RADIAN * .1) * .5;
		}
		
	}
}
import com.barliesque.agal.EasyAGAL;
import com.barliesque.agal.IField;
import com.barliesque.agal.IRegister;
import com.barliesque.agal.ISampler;
import com.barliesque.agal.TextureFlag;

class Shader2Vertices3Colors extends EasyAGAL
{
	override protected function _vertexShader():void
	{
		super._vertexShader();
		var position:IField = ATTRIBUTE[0];				//va0
		var color:IField = ATTRIBUTE[1];				//va1
		var texcoord:IField = ATTRIBUTE[2];				//va3
		var fragmentVar:IRegister = VARYING[0];			//v0
		var fragmentVar1:IRegister = VARYING[1];			//v1
		
		mov(OUTPUT,position);							//mov op,va0
		mov(fragmentVar,color);							//mov v0,va1
		mov(fragmentVar1,texcoord);						//mov v1,va2
	}
	
	override protected function _fragmentShader():void
	{
		super._fragmentShader();
		
		var fragmentVar:IRegister = VARYING[0];	//v0
		var texcoord:IRegister = VARYING[1];	//v1
		
		var out:IField = TEMP[0];	//ft0
		var sampler:ISampler = SAMPLER[0];
		
		var  tex_option:Array = [TextureFlag.TYPE_2D,TextureFlag.FILTER_LINEAR,TextureFlag.MIP_NO];
		tex(out,texcoord,sampler,tex_option);
		add(out,out,fragmentVar);
		mov(OUTPUT,out);	//mov oc,v0
	}
	

}